#include "cache.h"
#include "../data/account.h"
#include "../data/evaluation.h"
#include <QSettings>

Cache::Cache(QObject *parent, Account *account, Evaluation *evaluation) :
    QObject(parent),account(account),evaluation(evaluation) {
    qDebug()<<"Cache::Cache called.";
    autoLogin=false;
    storePassword=false;
    qDebug()<<"Cache::Cache returned.";
}

void Cache::load() {
    qDebug()<<"Cache::load called.";
    /* TODO: [Done] 从本地加载用户数据
     * 使用QSettings读取变量值，借此更新数据成员account和evaluation。
     */
    QSettings settings_Cache("AccountInfo.ini",QSettings::IniFormat);
    settings_Cache.setIniCodec("UTF8");
    settings_Cache.beginGroup("account");
    QVariant vUsername=settings_Cache.value("username");
    QVariant vPassword=settings_Cache.value("password");
    QVariant vIsAdmin=settings_Cache.value("isAdmin");
    QVariant vAutoLogin=settings_Cache.value("autoLogin");
    QVariant vStorePassword=settings_Cache.value("storePassword");
    if(!vUsername.isNull())
        account->setUsername(new QString(vUsername.toString()));
    if(!vPassword.isNull())
        account->setPassword(new QString(vPassword.toString()));
    if(!vIsAdmin.isNull()) account->isAdmin=vIsAdmin.toBool();
    else account->isAdmin=false;
    if(!vAutoLogin.isNull()) autoLogin=vAutoLogin.toBool();
    else autoLogin=false;
    if(!vStorePassword.isNull()) storePassword=vStorePassword.toBool();
    else storePassword=false;
    settings_Cache.endGroup();
    //读取Evaluation相关内容
    settings_Cache.beginReadArray("Evaluation");
    //读取method放入QStringList method中
    int m=settings_Cache.beginReadArray("method");
    if(m>0) {
        evaluation->setMethod(new QStringList);
        for(int i=0; i<m; i++) {
            settings_Cache.setArrayIndex(i);
            QString temp=settings_Cache.value("method").toString();
            evaluation->method->append(temp);
        }
    }
    settings_Cache.endArray();
    //读取target放入QStringList target中
    m=settings_Cache.beginReadArray("target");
    if(m>0) {
        evaluation->setTarget(new QStringList);
        for(int i=0; i<m; i++) {
            settings_Cache.setArrayIndex(i);
            QString temp=settings_Cache.value("target").toString();
            evaluation->target->append(temp);
        }
    }
    settings_Cache.endArray();
    //读取score
    m=settings_Cache.beginReadArray("score");
    if(m>0) {
        evaluation->setScore(new QVector<QVector<double>>);
        for(int i=0; i<m; i++) {
            QVector<double> temp;
            settings_Cache.setArrayIndex(i);
            int n=settings_Cache.beginReadArray(QString::number(i));
            for(int j=0; j<n; j++) {
                settings_Cache.setArrayIndex(j);
                temp.append(settings_Cache.value("score").toDouble());
            }
            settings_Cache.endArray();
            evaluation->score->append(temp);
        }
    }
    settings_Cache.endArray();
    settings_Cache.endArray();
    qDebug()<<"Cache::load returned.";
}

void Cache::save() {
    qDebug()<<"Cache::save called.";
    /* TODO: [Done] 将用户数据保存到本地
     * 使用QSettings设置变量，存储数据成员account和evaluation的值。
     */
    QSettings settings_Cache("AccountInfo.ini",QSettings::IniFormat);
    settings_Cache.clear();//将原来文件中的内容都清除掉。。
    QVariant vUsername,vPassword,vIsAdmin,vAutoLogin,vStorePassword;
    if(account->username!=nullptr) vUsername.setValue(*account->username);
    if(account->password!=nullptr) vPassword.setValue(*account->password);
    vIsAdmin.setValue(account->isAdmin);
    vAutoLogin.setValue(autoLogin);
    vStorePassword.setValue(storePassword);
    //在account进行存储。
    settings_Cache.beginGroup("account");
    settings_Cache.setValue("username",vUsername);
    settings_Cache.setValue("password",vPassword);
    settings_Cache.setValue("isAdmin",vIsAdmin);
    settings_Cache.setValue("autoLogin",vAutoLogin);
    settings_Cache.setValue("storePassword",vStorePassword);
    settings_Cache.endGroup();
    //写
    settings_Cache.beginGroup("Evaluation");
    settings_Cache.beginWriteArray("method");
    if(evaluation->method!=nullptr && !evaluation->method->isEmpty()) {
        for(int i=0; i<evaluation->method->length(); i++) {
            settings_Cache.setArrayIndex(i);
            QVariant temp;
            temp.setValue(evaluation->method->at(i));
            settings_Cache.setValue("method",temp);
        }
    }
    settings_Cache.endArray();
    settings_Cache.beginWriteArray("target");
    if(evaluation->target!=nullptr && !evaluation->target->isEmpty()) {
        for(int i=0; i<evaluation->target->length(); i++) {
            settings_Cache.setArrayIndex(i);
            QVariant temp;
            temp.setValue(evaluation->target->at(i));
            settings_Cache.setValue("target",temp);
        }
    }
    settings_Cache.endArray();
    settings_Cache.beginWriteArray("score");
    if(evaluation->score!=nullptr && !evaluation->score->isEmpty()) {
        for(int i=0; i<evaluation->score->length(); i++) {
            settings_Cache.setArrayIndex(i);
            settings_Cache.beginWriteArray(QString::number(i));
            for(int j=0; j<evaluation->score->at(i).length(); j++) {
                settings_Cache.setArrayIndex(j);
                settings_Cache.setValue("score",evaluation->score->at(i).at(j));
            }
            settings_Cache.endArray();
        }
    }
    settings_Cache.endArray();
    settings_Cache.endGroup();
    settings_Cache.sync();
    qDebug()<<"Cache::save returned.";
}
